package in.smartbillpay.web.controllers;

import in.smartbillpay.web.models.Authority;
import in.smartbillpay.web.services.AuthorityService;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;



/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/secure/authority")
public class AuthorityController {
	
	private static final Logger logger = LoggerFactory.getLogger(AuthorityController.class);
	
	@Autowired
	private AuthorityService authorityService;
	
	
	/**
	 * @return the logger
	 */
	public static Logger getLogger() {
		return logger;
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView createNewAuthority() {
		logger.info("Inside getAuthorities()");
				
		return new ModelAndView("authority_manager", "authority", new Authority());
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView addNewAuthority(Authority authority) {
		logger.info("Inside addNewAuthorities()");
		getAuthorityService().save(authority);
		logger.info("Saved New Authority()");
		return new ModelAndView("authority_manager", "authority", authority);
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public ModelAndView getAllAuthorities() {
		logger.info("Inside getAllAuthorities()");
		List<Authority> authorities =	getAuthorityService().getAllAuhtorities();
		logger.info("Authority count is- " + authorities.size() );
		ModelAndView mav = new ModelAndView("authority_manager");
		mav.addObject("authoritiesList", authorities);
		return  mav;
	}

	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}
	
	
}
